package com.project.lk.nurseapp.remind;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.common.service.AlarmReceiver;
import com.project.lk.nurseapp.db.RemindBean;
import com.project.lk.nurseapp.db.RemindBeanDao;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.RemindInfoUtils;
import com.project.lk.nurseapp.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class RemindActivity extends BaseActivity {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private List<RemindBean> mList=new ArrayList<>();
    private RemindAdapter adapter;


    @Override
    protected void initData() {

        adapter = new RemindAdapter(mList,this);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setLayoutManager(manager);

        adapter.setOnItemEventListenter(new RemindAdapter.OnItemEventListenter() {
            @Override
            public void showClick(int positon,boolean isOpen) {
                Log.e("MLOG",positon+">>>>"+isOpen);
                RemindBean remindBean = RemindInfoUtils.getUserInfoFromCache(positon+1 + "");
                remindBean.setIsOpen(isOpen);
                RemindBeanDao remindBeanDao = BaseApp.getInstance().getDaoSession().getRemindBeanDao();
                remindBeanDao.save(remindBean);
                if (isOpen){
                    RemindBean bean = adapter.getList().get(positon);
                    String[] split = bean.getTime().split(":");
                    String remindContext = bean.getRemindContext();
                    startAlarm(split[0],split[1],bean.getDateStr(),positon, remindContext);
                }else {
                    cancelAlarm(positon); //关闭闹钟
                }

            }
        });
    }

    public  void getPendingIntent(int alarmId){

    }
    private AlarmManager alarmManager;
    PendingIntent pendingIntent;
    //设置闹钟（在多长时间后闹钟响然后继续计时）
    public void startAlarm(String hour,String minute,String circleTime,int alarmId,String remindContext){
        //获取选择的闹钟时间
        Calendar c=Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        c.set(Calendar.MINUTE, Integer.parseInt(minute));
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        //给每个闹钟设置不同ID防止覆盖
//        int alarmId = (int) SharedPreferencesUtils.getParam("alarm_id", 0);
//        SharedPreferencesUtils.setParam("alarm_id", alarmId);
        Intent intent=new Intent(BaseApp.getContext(),AlarmReceiver.class);
        intent.setAction("alarm_receiver");
        intent.putExtra("remindContext",remindContext);
        intent.putExtra("alarmId",String.valueOf(alarmId));
        pendingIntent = PendingIntent.getBroadcast(BaseApp.getContext(), alarmId, intent, 0);
        //实例化闹钟个管理器
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        //闹钟什么时候继续执行
        long weekCircle = TimeUtils.getWeekCircle(circleTime, c);
        Log.e("MLOG",weekCircle+">>>>>>>>>>>>>>");
        alarmManager.set(AlarmManager.RTC_WAKEUP,weekCircle,pendingIntent);
    }
    //关闭周期闹钟
    public void cancelAlarm(int alarmId){
        //实例化闹钟个管理器
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//        int alarmId = (int) SharedPreferencesUtils.getParam("alarm_id", alarmId);
//        SharedPreferencesUtils.setParam("alarm_id", ++alarmId);
        Intent intent=new Intent(BaseApp.getContext(),AlarmReceiver.class);
        intent.setAction("alarm_receiver");
        pendingIntent = PendingIntent.getBroadcast(BaseApp.getContext(), alarmId, intent, 0);
        alarmManager.cancel(pendingIntent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        RemindBeanDao remindBeanDao = BaseApp.getInstance().getDaoSession().getRemindBeanDao();
        if (remindBeanDao != null) {
            mList = remindBeanDao.loadAll();
            adapter.setList(mList);
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_remind;
    }



    @OnClick({R.id.back, R.id.remind_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.remind_add: //添加提醒
                startActivity(new Intent(this, AddRemindActivity.class));
                break;
        }
    }




}
